package com.utooo.service.quality;


import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.utooo.dao.quality.CorrectiveActionVerificationMapper;
import com.utooo.dao.quality.GeneralRectificationMapper;
import com.utooo.dao.quality.MultiRectificationMapper;
import com.utooo.dao.quality.TeamMemberMapper;
import com.utooo.dao.supplier.quality.SupplierGeneralRectificationMapper;
import com.utooo.dao.supplier.quality.SupplierMultiRectificationMapper;
import com.utooo.pojo.domain.quality.CorrectiveActionVerificationDO;
import com.utooo.pojo.domain.quality.GeneralRectificationDO;
import com.utooo.pojo.domain.quality.MultiRectificationDO;
import com.utooo.pojo.domain.quality.TeamMemberDO;
import com.utooo.pojo.domain.supplier.quality.SupplierGeneralRectificationDO;
import com.utooo.pojo.domain.supplier.quality.SupplierMultiRectificationDO;
import com.utooo.pojo.dto.quality.RectificationPushDto;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

import static com.utooo.constant.Constans.IGNORED_PROPERTIES;

/**
 * @Description: TODO
 * @Author SJD
 * @Date: 2025/4/28 10:39
 * @Copyright: 南京优通信息科技股份有限公司
 */

@Service
@RequiredArgsConstructor
public class RectificationServiceImpl implements RectificationService{

    private final CorrectiveActionVerificationMapper correctiveActionVerificationMapper;
    private final SupplierMultiRectificationMapper supplierMultiRectificationMapper;
    private final TeamMemberMapper teamMemberMapper;
    private final MultiRectificationMapper buyerMultiRectificationMapper;
    private final GeneralRectificationMapper buyerGeneralRectificationMapper;
    private final SupplierGeneralRectificationMapper supplierGeneralRectificationMapper;

    @Override
    public Boolean pushCorrectiveAction(RectificationPushDto param) {
        // 获取隔壁整改单号
        MultiRectificationDO buy = buyerMultiRectificationMapper.selectOne(new LambdaQueryWrapper<MultiRectificationDO>().eq(MultiRectificationDO::getRectificationNo, param.getRectificationNo()));
        SupplierMultiRectificationDO supplier = BeanUtil.copyProperties(buy, SupplierMultiRectificationDO.class, IGNORED_PROPERTIES);
        supplierMultiRectificationMapper.insert(supplier);

        // 获取采购方的小组成员
        List<TeamMemberDO> teamMemberDOS = teamMemberMapper.selectList(new LambdaQueryWrapper<TeamMemberDO>().eq(TeamMemberDO::getForeignId, buy.getId()));
        teamMemberDOS.forEach(teamMemberDO -> {
            teamMemberDO.setId(null);
            teamMemberDO.setCreateTime(LocalDateTime.now());
            teamMemberDO.setForeignId(supplier.getId());
        });
        teamMemberMapper.insert(teamMemberDOS);
        // 获取采购方的纠正措施验证
        List<CorrectiveActionVerificationDO> actionVerificationDOS = correctiveActionVerificationMapper.selectList(new LambdaQueryWrapper<CorrectiveActionVerificationDO>().eq(CorrectiveActionVerificationDO::getForeignId, buy.getId()));
        actionVerificationDOS.forEach(actionVerificationDO -> {
            actionVerificationDO.setId(null);
            actionVerificationDO.setCreateTime(LocalDateTime.now());
            actionVerificationDO.setForeignId(supplier.getId());
        });
        correctiveActionVerificationMapper.insert(actionVerificationDOS);
        return true;
    }

    @Override
    public Boolean pushGeneralAction(RectificationPushDto param) {
        // 获取隔壁整改单号
        GeneralRectificationDO buy = buyerGeneralRectificationMapper.selectOne(new LambdaQueryWrapper<GeneralRectificationDO>().eq(GeneralRectificationDO::getRectificationNo, param.getRectificationNo()));
        SupplierGeneralRectificationDO supplier = BeanUtil.copyProperties(buy, SupplierGeneralRectificationDO.class, IGNORED_PROPERTIES);
        supplierGeneralRectificationMapper.insert(supplier);

        // 获取采购方的小组成员
        List<TeamMemberDO> teamMemberDOS = teamMemberMapper.selectList(new LambdaQueryWrapper<TeamMemberDO>().eq(TeamMemberDO::getForeignId, buy.getId()));
        teamMemberDOS.forEach(teamMemberDO -> {
            teamMemberDO.setId(null);
            teamMemberDO.setCreateTime(LocalDateTime.now());
            teamMemberDO.setForeignId(supplier.getId());
        });
        teamMemberMapper.insert(teamMemberDOS);
        // 获取采购方的纠正措施验证
        List<CorrectiveActionVerificationDO> actionVerificationDOS = correctiveActionVerificationMapper.selectList(new LambdaQueryWrapper<CorrectiveActionVerificationDO>().eq(CorrectiveActionVerificationDO::getForeignId, buy.getId()));
        actionVerificationDOS.forEach(actionVerificationDO -> {
            actionVerificationDO.setId(null);
            actionVerificationDO.setCreateTime(LocalDateTime.now());
            actionVerificationDO.setForeignId(supplier.getId());
        });
        correctiveActionVerificationMapper.insert(actionVerificationDOS);
        return true;
    }
}
